/*
 * Copyright 2018 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */

package com.zhongan.cloud.gateway.filter.previous;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * CommonGwRoutConfig Dao操作接口
 *
 * @author tongyufu
 * @since 2018年7月4日 下午2:13:15
 */
@Repository
public class CommonGwRoutConfigDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    private String       baseColumn = "id, service_name, service_version, url_route, service_desc, service_owner, forward_protocol,"
                                            + " app_name, app_url, app_request_head, app_response_class, interface_name, interface_method,"
                                            + " interface_version, response_head, `status`, print_forward_log, is_deleted, creator,"
                                            + " modifier, gmt_created, gmt_modified ";

    public List<CommonGwRoutConfig> queryGwConfig(String serviceName, String serviceVersion, String urlRoute) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select ");
        sql.append(baseColumn);
        sql.append(" from common_gw_rout_config where is_deleted = 'N'");
        sql.append(" and service_name = ?");
        params.add(serviceName);
        if (StringUtils.isNotBlank(serviceVersion)) {
            sql.append(" and service_version = ?");
            params.add(serviceVersion);
        }
        if (StringUtils.isNotBlank(urlRoute)) {
            sql.append(" and url_route = ?");
            params.add(urlRoute);
        }
        return jdbcTemplate.query(sql.toString(), params.toArray(), new BeanPropertyRowMapper<>(
                CommonGwRoutConfig.class));
        //return jdbcTemplate.queryForList(sql.toString(), CommonGwRoutConfig.class, params.toArray());
    }
}
